package com.betty.cscams.controller;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.entity.Warning;
import com.betty.cscams.service.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ---------------------------
 *  智能预警信息控制器(WarningController)         
 * ---------------------------
 */
@RestController
@RequestMapping("warning")
public class WarningController {

    @Autowired
    private WarningService warningService;

    /**
     * 保存预警信息
     * @param record 预警信息
     * @return 操作结果
     */
    @PostMapping(value = "/save")
    @PreAuthorize("hasAuthority('sys:warning:add') OR hasAuthority('sys:warning:edit')")
    public HttpResult save(@RequestBody Warning record) {
        return HttpResult.ok(warningService.save(record));
    }

    /**
     * 删除预警信息
     * @param records 预警信息列表
     * @return 操作结果
     */
    @PostMapping(value = "/delete")
    @PreAuthorize("hasAuthority('sys:warning:delete')")
    public HttpResult delete(@RequestBody List<Warning> records) {
        return HttpResult.ok(warningService.delete(records));
    }

    /**
     * 根据ID查询预警信息
     * @param id 预警ID
     * @return 预警信息
     */
    @GetMapping(value = "/findById")
    public HttpResult findById(@RequestParam Long id) {
        return HttpResult.ok(warningService.findById(id));
    }

    /**
     * 分页查询预警信息
     * @param pageRequest 分页请求
     * @return 分页结果
     */
    @PostMapping(value = "/findPage")
    public HttpResult findPage(@RequestBody PageRequest pageRequest) {
        return HttpResult.ok(warningService.findPage(pageRequest));
    }

    /**
     * 获取当前用户的预警信息
     * @param status 状态（可选）
     * @return 预警信息列表
     */
    @GetMapping(value = "/findCurrentUserWarnings")
    public HttpResult findCurrentUserWarnings(@RequestParam(required = false) Integer status) {
        return HttpResult.ok(warningService.findCurrentUserWarnings(status));
    }

    /**
     * 统计当前用户未读预警数量
     * @param studentNum 学号
     * @return 未读预警数量
     */
    @GetMapping(value = "/countUnread")
    public HttpResult countUnread(@RequestParam String studentNum) {
        return HttpResult.ok(warningService.countUnreadByStudentNum(studentNum));
    }

    /**
     * 将预警标记为已读
     * @param id 预警ID
     * @return 操作结果
     */
    @PostMapping(value = "/markAsRead")
    public HttpResult markAsRead(@RequestParam Long id) {
        return HttpResult.ok(warningService.markAsRead(id));
    }

    /**
     * 将所有预警标记为已读
     * @param studentNum 学号
     * @return 操作结果
     */
    @PostMapping(value = "/markAllAsRead")
    public HttpResult markAllAsRead(@RequestParam String studentNum) {
        return HttpResult.ok(warningService.markAllAsRead(studentNum));
    }

    /**
     * 创建心理测评预警
     * @param studentNum 学号
     * @param termId 学期ID
     * @param score 分数
     * @return 操作结果
     */
    @PostMapping(value = "/createPsyWarning")
    @PreAuthorize("hasAuthority('sys:warning:add')")
    public HttpResult createPsyWarning(@RequestParam String studentNum, 
                                      @RequestParam Long termId, 
                                      @RequestParam int score) {
        return HttpResult.ok(warningService.createPsyWarning(studentNum, termId, score));
    }
} 